/* eslint-disable */
import api from './http'
import { getUrlParam } from '@/utils/helper'
import { Dialog,Toast } from 'vant';
import { withLoading, withConfirm } from '@/decorate'
import { translate } from "@/utils/helper.js"
import { postFile } from './http/index'
import { CancelToken } from './http'

let changeLanguage = translate()
const ticket = getUrlParam('ticket')
const appid = getUrlParam('appid') || getUrlParam('client_id') || getUrlParam('appId')
class Api {
  /**
   * 入口
   * @param {*} data 
   */

  // 获取ticket
  getticket(eid,oid){
    return api.get('/base/generteTicket',{
      params: {
        eid,
        oid
      }
    })
  }

   // 获取文件预览URL
   getPreviewUrl(messageId, attachmentId) {
    let cancel;
    let timer = setTimeout(() => {
      Toast.clear();
      console.warn("当前goloading",changeLanguage.goloading);
      Dialog.confirm({
        message:changeLanguage.goloading,
        confirmButtonText: changeLanguage.yes,
        cancelButtonText: changeLanguage.no,
        className: "errSelfDialog",
      })
        .then(() => {
          console.warn(this)
          // console.warn(Toast.loading());
          Toast.loading({
            message: changeLanguage.loading,
            forbidClick: true,
            duration: 0,
          });
          console.warn("继续等待");
          // return;
        })
        .catch((e) => {
          console.warn("停止等待",e);
          cancel();
        });
    }, 5000);
  
    return api.get('/attachment/getPreViewUrl', {
      params: {
        messageId,
        attachmentId
      },
      cancelToken: new CancelToken(function executor(c) {
        cancel = c;
      })
    }).then((response) => {
      clearTimeout(timer);
      Dialog.close();
      return response;
    })
    .catch((error) => {
        clearTimeout(timer);
        console.warn("数据获取失败",error);
        return error
    });
 
  }

  //续费cookie
  cookrefresh() { return api.get('/base/refresh') }

  //获取token
  getToken(params) {
    return api.get(`/base/index`, params)
  }

  //搜索人员
  searchPeople(param){
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick: true,
    })
    return api.get('/personOrg/search',{
      params:{...param}
    })
  }
  //--------------TT相关---------
  /** 取消代办*/
  notificationCancle(sourceId) {
    return api.post(`/notification/cancelTodo/${sourceId}`)
  }

  //-----------------------------------------
  /**获取主页邮件列表 20条 */
  listMeByFolderId(params) {
    return api.post(`/message/listMeByFolderId`, params)
  }


  /**文件夹相关 */
  //展示子文件夹
  listChildFolder(parentId) {
    return api.get(`/folder/listChildFolder`, {
      params: {
        parentId
      }
    })
  }
  //展示当前文件夹
  listCurrentFolder() {
    return api.get(`/folder/selfFolder`)
  }
  //获取文件夹信息
  getFolderInfo(folderId) {
    return api.get(`/folder/getFolder`, {
      params: {
        folderId
      }
    })
  }

  //获取邮件详情
  getMailDetail(id) {
    return api.get(`/message/getMessageDetail`, {
      params: {
        id: id
      }
    })
  }

  //关注邮件
  controllFollow(id) {
    return api.get(`/message/controlFollow`, {
      params: {
        id: id
      }
    })
  }

  /*******************写邮件相关**************************************** */
  //写邮件前
  createWriteNew() {
    return api.post(`/message/createNewMessage`, {})
  }
  //发送写好的邮件
  sendWriteNew(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/sendNewMessage`, requestBody)
  }
  //保存写邮件草稿
  saveNewDraft(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/saveNewMessage`, requestBody)
  }

  /***********************************************回复相关 **********************************************/

  // 获取快速回复常用语列表
  getQuickReply() {
    return api.get('/reply/tip/list')
  }
  // 创建快速回复常用语
  creatQuickReplyUsual(tip) {
    // Toast.loading({
    //   message:changeLanguage.loading,
    //   forbidClick:true,
    //   duration:0
    //  })
    return api.post(`/reply/tip/create`, {
      tip
    })
  }
  // 删除快速回复常用语

  delQuickReplyUsual(replyTipId) {
        Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/reply/tip/${replyTipId}delete`)
  }
  // 发送快速回复邮件
  quickReply(params) {
    Toast.loading({
      message:changeLanguage.sending,
      forbidClick:true,
      duration:0
     })
    return api.post('/reply/quickReply', params)
  }
  //正式回复前创建草稿
  @withLoading({ message: `${changeLanguage.loading} ` })
  createReply(messageId) {
    // Toast.loading(changeLanguage.loading)
    return api.post(`/reply/${messageId}/createReply`)
  }
  //发送正式回复
  sendReply(requestBody) {
    return api.post(`/reply/sendReply`, requestBody)
  }
  //保存正式回复
  saveReply(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/reply/saveReply`, requestBody)
  }
  // 删除正式回复delReply
  @withConfirm({ message: changeLanguage.sureDel })
  delReply(messageId) {
    return api.post(`/reply/${messageId}/deleteReply`)
  }

  //----------------------转发相关----------
  // 创建转发接口
  creatTransmit(id) {
    // Toast.loading(changeLanguage.loading)
    return api.get('/forwardMessage/create', {
      params: {
        id: id
      }
    })
  }
  // 更新并发送转发接口
  sendTransmit(requestBody) {
    Toast.loading(changeLanguage.sending)
    return api.post('/forwardMessage/send', requestBody)
  }
  // 更新转发
  updateTransmit(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post('/forwardMessage/update', requestBody)
  }

  // 删除转发
  @withConfirm({ message: changeLanguage.sureDel })
  delForwardDraft(id) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.get('/forwardMessage/delMessage', {
      params: {
        id: id
      }
    })
  }






  /**********************************************附件相关 start ~****************************************/
  /**
   * 附件和每封邮件相关
   */
  // 大文件上传 超过4m文件分片处理
  bigFileUpload(requestBody) {
    return api.post(`/attachment/createUploadSession?messageId=${requestBody.messageId}&name=${requestBody.name}&size=${requestBody.size}`)
  }
  // 分片上传
  fragmentUpload(url, requestBody, header) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return postFile(url, requestBody, header)
  }

  // 删除附件
  // @withConfirm({ message: changeLanguage.sureDel })
  delAttachment(parmas) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.get(`/attachment/delete`, {
      params: {
        ...parmas
      }
    })
  }

  //小于4m文件上传
  uploadFile(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/attachment/uploadByFile`, requestBody)
  }

  //获取文件流
  // getAttachmentByte(messageId, attachmentId){
  //   return api.post(`/attachment/getAttachmentByte?messageId=${messageId}&attachmentId=${attachmentId}`)
  // }

  /**********************************************附件相关 end ~****************************************/


  /** 主页管理相关 **/
  //批量已读
  batchRead(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/batchRead`, requestBody)
  }



  //批量关注
  batchFollow(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/batchFollow`, requestBody)
  }

  //批量删除已删除文件夹邮件
  @withConfirm({ message: changeLanguage.sureDel })
  batchDelete(arr) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/delMessages`, arr)
  }
  // 批量删除其他文件夹邮件
  // @withConfirm({ message: changeLanguage.sureDel })
  batchMoveDel(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/moveMessage`, requestBody)
  }
  //批量移动
  batchMove(requestBody) {
    Toast.loading({
      message:changeLanguage.loading,
      forbidClick:true,
      duration:0
     })
    return api.post(`/message/moveMessage`, requestBody)
  }




  /**oppo ~~~~~~~~~~~~~~~~~~~end~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/



}
export default new Api()
